package org.sxf.app.sign.biz;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;

import com.alibaba.fastjson.JSON;
import com.lidroid.xutils.util.LogUtils;

import org.sxf.app.sign.R;
import org.sxf.app.sign.activity.SplashActivity;
import org.sxf.app.sign.data.DataCacheSingleton;
import org.sxf.app.sign.handler.APPHandler;
import org.sxf.app.sign.model.MsgRepBean;
import org.sxf.app.sign.provider.MsgHandler;
import org.sxf.app.sign.utils.UIUtils;
import org.sxf.app.sign.utils.Utility;

/**
 * Created by zqc on 17/1/16.
 */
public class BaseBiz {
    public interface OnBizCallBackListener{
        void onBizResult(int biz, Object result);
        void onBizError(int biz, Object result);
    }


    private String RS_SUCCESS = "0";
    private String RS_TOKEN_WRONG = "FFE3";
    private String RS_TOKEN_EXPIRED = "FFE4";

    public final static int ERROR_TOKEN_EXPIRED = 0xE2;
    public final static int ERROR_UNKNOWN_FAIL = 0xE1;
    public final static int ERROR_SIGNATURE_FAIL = 0xE0;

    public final static int ERROR_NET_FAIL = 0xEE0;

    Context mContext;
    OnBizCallBackListener mBizCallBackListener;

    boolean mIsQuit = false;

    public void stopAll(){
        mIsQuit = true;
    }


    MsgRepBean checkMsg(String msgStr, boolean shouldCallBack){
        String repStr;
        MsgRepBean repBean = null;
        try {
            repStr = MsgHandler.msgDecrypt(msgStr);
            repBean = JSON.parseObject(repStr, MsgRepBean.class);

            if (RS_TOKEN_WRONG.equalsIgnoreCase(repBean.rs)){
                if (shouldCallBack){
//                    safeOnBizError(ERROR_TOKEN_EXPIRED, null);


                    DataCacheSingleton.getInstance().clearAllData();
                    APPHandler.getInstance().clearAllBackgroundActivity();
                    Intent intent = new Intent(APPHandler.getInstance().getCurrentActivity(),  SplashActivity.class);
                    intent.putExtra("s_logout", UIUtils.getString(R.string.other_dev_login));
                    APPHandler.getInstance().getCurrentActivity().startActivity(intent);
                    APPHandler.getInstance().getCurrentActivity().overridePendingTransition(R.anim.act_null, R.anim.act_fade_out);
                    APPHandler.getInstance().getCurrentActivity().finish();


                }
                return null;
            }else if (RS_TOKEN_EXPIRED.equalsIgnoreCase(repBean.rs)){
                if (shouldCallBack){
//                    safeOnBizError(ERROR_TOKEN_EXPIRED, null);

                    DataCacheSingleton.getInstance().clearAllData();
                    APPHandler.getInstance().clearAllBackgroundActivity();
                    Intent intent = new Intent(APPHandler.getInstance().getCurrentActivity(),  SplashActivity.class);
                    intent.putExtra("s_logout", UIUtils.getString(R.string.login_expired));
                    APPHandler.getInstance().getCurrentActivity().startActivity(intent);
                    APPHandler.getInstance().getCurrentActivity().overridePendingTransition(R.anim.act_null, R.anim.act_fade_out);
                    APPHandler.getInstance().getCurrentActivity().finish();


                }
                return null;
            }else if (!RS_SUCCESS.equalsIgnoreCase(repBean.rs)){
//                if (shouldCallBack) {
//                    safeOnBizError(ERROR_UNKNOWN_FAIL, null);
//                }
                return null;
            }
            if (!MsgHandler.checkSignature(repBean)){
//                if (shouldCallBack) {
//                    safeOnBizError(ERROR_SIGNATURE_FAIL, null);
//                }
                return null;
            }

        }catch (Exception e){
            LogUtils.e(Utility.getStackTrace(e));
//            if (shouldCallBack) {
//                safeOnBizError(ERROR_UNKNOWN_FAIL, null);
//            }
            return null;
        }
        return repBean;
    }



    void safeOnBizError(int biz, Object data){
        if (mBizCallBackListener != null){
            mBizCallBackListener.onBizError(biz, data);
        }
    }

    void safeOnBizResult(int biz, Object data){
        if (mBizCallBackListener != null){
            mBizCallBackListener.onBizResult(biz, data);
        }
    }

    public void showAlertDialog(String title, String detail){
        AlertDialog alertDialog = new AlertDialog.Builder(APPHandler.getInstance().getCurrentActivity()).setTitle(title)
                .setMessage(detail)
                .setPositiveButton(UIUtils.getResources().getString(R.string.confirm), new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {

                    }
                }).create();
        alertDialog.show();
    }

}
